import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/8 10:57
 * @description DP优化，由于dp[i] 仅和dp[i-1]、dp[i-2]有关，可以用常量保存
 */
public class Solution2 {
    public static void main(String[] args) {

    }
    public int numDecodings(String s) {
        int n = s.length();
        // a = f[i-2], b = f[i-1], c=f[i]
        int a = 0, b = 1, c = 0;
        for (int i = 1; i <= n; ++i) {
            c = 0;
            if (s.charAt(i - 1) != '0') {
                c += b;
            }
            if (i > 1 && s.charAt(i - 2) != '0' && ((s.charAt(i - 2) - '0') * 10 + (s.charAt(i - 1) - '0') <= 26)) {
                c += a;
            }
            a = b;
            b = c;
        }
        return c;
    }
}
